package com.xmut.mall.order.controller;

import com.alibaba.fastjson.JSON;
import com.xmut.mall.order.utils.RespBean;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.client.exception.MQBrokerException;
import org.apache.rocketmq.client.exception.MQClientException;
import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.rocketmq.client.producer.SendCallback;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.common.message.Message;
import org.apache.rocketmq.common.message.MessageExt;
import org.apache.rocketmq.remoting.exception.RemotingException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Map;

@RestController
@Slf4j
@RequestMapping("/test")
public class TestController {

    @Autowired
    private HttpServletRequest request;


    @GetMapping("/testFeignJwt")
    public RespBean testFeignJwt() {
        String accessToken = request.getHeader("accessToken");
        String refreshToken = request.getHeader("refreshToken");
        return RespBean.success("成功", accessToken + " " + refreshToken);
    }

    @GetMapping("/product/send")
    public String sendMessage()  {
        log.info("发送消息");
        return "发送成功";
    }

    @GetMapping("/testSend")
    public void testSend(){
        for (int i = 0; i < 1000; i++) {
            Message message = new Message();
            message.setTopic("OrderLockStockTopic");
            String body = "测试发送:"+ i;
            message.setBody(body.getBytes(StandardCharsets.UTF_8));
            message.setDelayTimeLevel(3);
        }
    }

    @GetMapping("/testSend1")
    public void testSend1(){
        DefaultMQProducer producer = new DefaultMQProducer("ordergroup");
        producer.setNamesrvAddr("192.168.253.128:9876");
        try {
            producer.start();
            for (int i = 0; i < 10000; i++) {
//                String data = "{\"title\":\"X市2021年度第四季度税务汇总数据\"}";
//                Message message = new Message("tax-data", "2021S4", data.getBytes());
//                SendResult result = producer.send(message);
//                System.out.println("消息已发送：MsgId:" + result.getMsgId() + "，发送状态:" + result.getSendStatus());
                Message message = new Message();
                String data = "测试发送:" + i;
                message.setTopic("OrderLockStockTopic");
                String body = "测试发送:"+ i;
                Map<String,Object> param = new HashMap<>();
                param.put("SN",body);
                message.setBody(JSON.toJSONString(param).getBytes(StandardCharsets.UTF_8));
                message.setDelayTimeLevel(3);
                SendResult result = producer.send(message);
                System.out.println("消息已发送：MsgId:" + result.getMsgId() + "，发送状态:" + result.getSendStatus());
            }
        } catch (MQClientException | RemotingException | MQBrokerException | InterruptedException e) {
            e.printStackTrace();
        } finally {
            try {
                producer.shutdown();
                System.out.println("链接已关闭");
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}
